package com.zhenai.rocyuan.commons.rbac;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.zhenai.rocyuan.commons.uitls.FixCommonUtils;
import com.zhenai.rocyuan.commons.uitls.cookie.WorkerSessionManager;
import com.zhenai.rocyuan.modules.rbac.service.RbacService;

public class AccessControl {
	private static RbacService rbacService;

	public static boolean roleBasedControlAccess(String uri, HttpServletRequest request) {
		Integer currentWorkerId = WorkerSessionManager.getWorkerId(request);
		String[] uris = StringUtils.split(uri, ",");
		if(uris!=null && uris.length>1) {
			for (String u : uris) {
				boolean hasUri = rbacService.hasUri(currentWorkerId, u);
				if(hasUri)	return true;
			}
			return false;
		}
		return rbacService.hasUri(currentWorkerId, uri);
	}

	public static boolean roleBasedControlAccess(String uri, HttpServletRequest request,
			int typeId, int rangeValue) {
		Integer currentWorkerId = WorkerSessionManager.getWorkerId(request);
		return roleBasedControlAccess(uri, currentWorkerId, typeId, rangeValue);
	}

	public static boolean roleBasedControlAccess(String uri, HttpServletRequest request,
			int typeId, String rangeValues) {
		String[] ids = rangeValues.split(",");
		Integer currentWorkerId = WorkerSessionManager.getWorkerId(request);
		return rbacService.hasUri(currentWorkerId, uri, typeId, FixCommonUtils.evalIntArray(ids));
	}

	public static boolean roleBasedControlAccess(String uri, HttpServletRequest request,
			int typeId, int[] rangeValues) {
		Integer currentWorkerId = WorkerSessionManager.getWorkerId(request);
		return rbacService.hasUri(currentWorkerId, uri, typeId, rangeValues);
	}
	
	public static boolean roleBasedControlAccess(String uri, int currentWorkerId, int typeId, int rangeValue) {
		return rbacService.hasUri(currentWorkerId, uri, typeId, new int[]{rangeValue});
	}


	public static int getValueByRange(String uri, HttpServletRequest request, int defaultValue) {
		int temp = defaultValue;
		Integer currentWorkerId = WorkerSessionManager.getWorkerId(request);
		int rangeId = rbacService.getRangeValue(currentWorkerId, uri);
		return rangeId == -1 ? temp : rangeId;
	}
	
	public static int getValueByType(String uri, HttpServletRequest request) {
		int temp = 0;
		Integer currentWorkerId = WorkerSessionManager.getWorkerId(request);
		int rangeId = rbacService.getRangeValue(currentWorkerId, uri);
		return rangeId == -1 ? temp : rangeId;
	}

	public static String getValuesByType(String uri, HttpServletRequest request, int type) {
		Integer currentWorkerId = WorkerSessionManager.getWorkerId(request);
		return getValuesByType(uri, currentWorkerId, type);
	}
	
	public static String getValuesByType(String uri, Integer currentWorkerId, int type) {
		List<Integer> list = rbacService.getValuesByType(currentWorkerId, uri, type);
		if(list != null && list.size()>0) {
			Set<Integer> sets = new HashSet<Integer>();
			sets.addAll(list);
			StringBuilder sb = new StringBuilder();
			for (Integer id : sets) {
				sb.append(id).append(",");
			}
			return sb.substring(0, sb.length()-1);
		}
		return StringUtils.EMPTY;
	}
	
	public static int getValueByType(String uri, HttpServletRequest request, int type) {
		Integer currentWorkerId = WorkerSessionManager.getWorkerId(request);
		return rbacService.getValueByType(currentWorkerId, uri, type);
	}

	public void setRbacService(RbacService rbacService) {
		AccessControl.rbacService = rbacService;
	}

}
